package com.atguigu.controller;


import com.atguigu.entity.BaseBrand;
import com.atguigu.result.RetVal;
import com.atguigu.service.BaseBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author yangchaolove
 * @since 2021-11-14
 *
 * http://127.0.0.1/product/brand/queryBrandByPage/1/10
 */
@RestController
@RequestMapping("/product/brand")
@Slf4j
public class BrandController {

    @Value("${fastdfs.prefix}")
    private String fastdfsprefix;

    @Autowired
    private BaseBrandService baseBrandService;
    //分页查询
    @GetMapping("/queryBrandByPage/{currentPageNum}/{pageSize}")
    public RetVal queryBrandByPage(@PathVariable Long currentPageNum,
                                   @PathVariable Long pageSize){
        //分页对象
        Page<BaseBrand> page = new Page<>(currentPageNum, pageSize);
        QueryWrapper<BaseBrand> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        baseBrandService.page(page,wrapper);
        return RetVal.ok(page);
    }
    //添加品牌
    @PostMapping
    public  RetVal  Save(@RequestBody BaseBrand baseBrand){
        baseBrandService.save(baseBrand);
        return RetVal.ok();
    }
    //修改时数据回显
    @GetMapping("/{id}")
    public  RetVal  Save(@PathVariable Long id){
        BaseBrand brand = baseBrandService.getById(id);
        return RetVal.ok(brand);
    }
    //更新品牌
    @PutMapping
    public  RetVal  updatebyId(@RequestBody BaseBrand baseBrand){
        baseBrandService.updateById(baseBrand);
        return RetVal.ok();
    }
    //删除品牌
    @DeleteMapping("/{id}")
    public  RetVal  delete(@PathVariable Long id) throws IOException, MyException {
        String configfilePath = this.getClass().getResource("/tracker.conf").getFile();
        log.info("tracker.conf路径为"+configfilePath);
        //初始化
        ClientGlobal.init(configfilePath);
        //创建客户端
        TrackerClient trackerClient = new TrackerClient();
        // 获取链接
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建strorgclient
        StorageClient1 storageClient = new StorageClient1(trackerServer, null);
        BaseBrand brand = baseBrandService.getById(id);
        String brandLogoUrl = brand.getBrandLogoUrl();
        log.info("分割前路径为："+brandLogoUrl);
        String path= brandLogoUrl.substring(24);
        log.info("分割后路径为："+path);
        storageClient.delete_file1(path);
        baseBrandService.removeById(id);
        return RetVal.ok();
    }
    //查询所有的品牌不分页
    @GetMapping("/getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> baseBrands = baseBrandService.list(null);
        return RetVal.ok(baseBrands);
    }
    @PostMapping("/fileUpload")
    public RetVal fileUpload(MultipartFile file) throws IOException, MyException {
        String configfilePath = this.getClass().getResource("/tracker.conf").getFile();
        log.info("tracker.conf路径为"+configfilePath);
        //初始化
        ClientGlobal.init(configfilePath);
        //创建客户端
        TrackerClient trackerClient = new TrackerClient();
        // 获取链接
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建strorgclient
        StorageClient1 storageClient = new StorageClient1(trackerServer, null);
        String originalFilename = file.getOriginalFilename();
        String extensionname = FilenameUtils.getExtension(originalFilename);

        //上传文件
        String filePath = storageClient.upload_appender_file1(file.getBytes(), extensionname, null);
        log.info("访问上传路径为"+fastdfsprefix+filePath);
        //http://yangchaolove.top/group1/M00/00/00/rBQocWGQ2rOEcEEfAAAAAFL5Jzk114.jpg
        return RetVal.ok(fastdfsprefix+filePath);
    }
}

